import { _decorator, Component, Node, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('CameraControl')
export class CameraControl extends Component {
  @property(Number)
  min:number = 0
  @property(Number)
  max: number = 3000
  player: Node = null
  start() {
    this.player = this.node.getParent().getChildByName('player')
  }

  update(deltaTime: number) {
    if (this.node.position.x < this.min) {
      this.node.setPosition(new Vec3(this.min, 0, 0))
    }
    if (this.player.position.x > this.min) {
      const { x, y, z } = this.player.position
      const { x: cx, y: cy, z: cz } = this.node.position
      this.node.setPosition(new Vec3(x, cy, cz))
    }
    if (this.player.position.x > this.max) {
      const { x, y, z } = this.player.position
      const { x: cx, y: cy, z: cz } = this.node.position
      this.node.setPosition(new Vec3(this.max, cy, cz))
    }
  }
}


